#include "IpAddress.h"
#include <string>

CIpAddress::CIpAddress()
{
    ;
}


CIpAddress::CIpAddress(std::string Ip, uint16_t port)
{
    IpAdress.sin_family = AF_INET;
    IpAdress.sin_port = htons(port);
    inet_pton(AF_INET, Ip.c_str(), &IpAdress.sin_addr);
}


CIpAddress::CIpAddress(uint32_t Ip, uint16_t port)
{
    IpAdress.sin_family = AF_INET;
    IpAdress.sin_port = htons(port);
    IpAdress.sin_addr.S_un.S_addr = htonl(Ip);
}


CIpAddress::~CIpAddress()
{
}


sockaddr* CIpAddress::GetIpAdressStrut()
{
    return (sockaddr *)&IpAdress;
}


uint32_t CIpAddress::GetIpAdress()
{
    return htonl(IpAdress.sin_addr.S_un.S_addr);
}


uint16_t CIpAddress::GetPort()
{
    return htons(IpAdress.sin_port);
}


int CIpAddress::GetSize()
{
    return sizeof(IpAdress);
}


std::string CIpAddress::ToString()
{
    char buffer[260];
    char address[128];
    inet_ntop(AF_INET, &IpAdress.sin_addr, address, sizeof(address));
    snprintf(buffer, sizeof(buffer), "%s:%d", address, htons(IpAdress.sin_port));
    return std::string(buffer);
}